﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ElfTyping.States.GameState.Entities
{
    public class TibarnEnemy : Enemy
    {
        const int CastingPositionX = 100;
        const int SummoningDistanceY = 60;

        // speed up casting as you get lower on health
        float TotalSecondsBetweenCasts
        {
            get
            {
                if (CurrentHealth <= MaximumHealth / 4)
                {
                    return 7;
                }
                else if (CurrentHealth <= MaximumHealth / 2)
                {
                    return 8;
                }
                else if (CurrentHealth <= MaximumHealth * 3 / 4)
                {
                    return 9;
                }
                else
                {
                    return 10;
                }
            }
        }
        float secondsUntilCast = 0;

        GameState.EventManager EventManager;

        public TibarnEnemy(GameScript script, Vector2 center, GameState.EventManager eventManager)
            : base(script, center, EnemyDifficulty.Hard)
        {
            LoadAnimations(@"Textures\GameState\WyvernLord_Red");
            MaximumHealth = 200;
            BaseSpeed = 10;
            secondsUntilCast = 0;// TotalSecondsBetweenCasts;
            Word = GetNewWord(6);

            EventManager = eventManager;
        }

        public override void Tick(float dt)
        {
            if (Script.GetEnemies().Count > 1)
            {
                IsActive = false;
            }
            else
            {
                IsActive = true;
            }

            if (CenterPosition.X < CastingPositionX) // moving until stop
            {
                MoveCenter(CenterPosition.X + MoveSpeed * dt, CenterPosition.Y);
                CurrentAnimation = MovingAnimation;
                base.Tick(dt);
            }
            else if (secondsUntilCast > 0) // waiting to cast
            {
                BaseSpeed = 0;
                secondsUntilCast -= dt;
                //CurrentAnimation = AttackingAnimation;
            }
            else // casting
            {
                int rand = random.Next(1, 101);
                float randomY = random.Next(160, 400);

                if (rand <= 30)
                {
                    Script.SpawnEnemy(new BasicEnemy(Script, new Vector2(-50, randomY), EnemyDifficulty.Easy));
                }
                else if (rand <= 60)
                {
                    Script.SpawnEnemy(new RogueEnemy(Script, new Vector2(random.Next(200, 400), randomY), EnemyDifficulty.Easy));
                    EventManager.SpawnEasyRogue();
                }
                else if (rand <= 80)
                {
                    Script.SpawnEnemy(new SummonerEnemy(Script, new Vector2(-50, randomY), EnemyDifficulty.Easy));
                }
                else if (rand <= 92)
                {
                    Script.SpawnEnemy(new RogueEnemy(Script, new Vector2(random.Next(200, 400), randomY), EnemyDifficulty.Medium));
                }
                else
                {
                    Script.SpawnEnemy(new BasicEnemy(Script, new Vector2(-50, randomY), EnemyDifficulty.Medium));
                }
                secondsUntilCast = TotalSecondsBetweenCasts;
            }
        }
    }
}
